var util = require("../../utils/util.js");
// pages/goods-personal/goods-personal.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        userInfo: '',
        isShowOptions: true,
        isShowRedPacket: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let friendOpenId;
        if (options.friendId) {
            friendOpenId = options.friendId
        }
        this.setData({
            friendOpenId:friendOpenId
        });
        this.getFriendInfo(friendOpenId);
        this.getOdrderList();
        const userInfo = wx.getStorageSync('userInfo');
        if (userInfo) {
            this.setData({
                userInfo
            });
            if(userInfo.openId == friendOpenId ){
                wx.navigateTo({
                    url: "/pages/goods-recommend/goods-recommend?freindId="+friendOpenId
                })
            }
            this.confirmAddCircle(friendOpenId);
        }
    },
    getUserInfo: function (e) {
        this.setData({
            userInfo: e.detail.userInfo
        })
    },
    confirmAddCircle: function(friendOpenId) {
        let _this = this;
        util.get(getApp().api.follow.confirmFollow,{masterId:friendOpenId}).then(
            function (res) {
                console.log('2222',res);
                if (res.code == 200) {
                    _this.setData({
                        isInvite: !!res.data //没邀请过值是false
                    });
                }  else{
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function () {
                        wx.hideToast();
                    }, 1500);
                }
            }
        );
    },
    invite:function(){
        const _this = this;
        const userInfo = wx.getStorageSync('userInfo');
        if (!userInfo) {
            _this.selectComponent('#login').openLoginModal();
            return false
        } else {
            _this.setData({
                userInfo
            })
        }
        _this.addCircle(_this.data.friendOpenId)
    },
    goToRecommend:function(){
        wx.navigateTo({
            url: "/pages/goods-recommend/goods-recommend?friendId="+this.data.friendOpenId
        })
    },
    loginListener:function(){
        this.getUserInfo();
        this.addCircle(this.data.friendOpenId);
    },
    closeListener:function(){

    },
    addCircle:function(friendOpenId){
        let _this = this;
        util.request(getApp().api.follow.addFollow, {masterId:friendOpenId},'POST').then(
            function(res) {
                if (res.code == 200) {
                    console.log(res.data);
                    _this.goToRecommend();
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                }
            }
        );
    },
    getFriendInfo(friendOpenId) {
        const _this = this;
        util.get(getApp().api.user.info,{openId:friendOpenId}).then(
            function (res) {
                console.log(res);
                if (res.code == 200) {
                    console.log(res.data);
                    _this.setData({
                        friendInfo: res.data || []
                    });
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function () {
                        wx.hideToast();
                    }, 1500);
                }
            }
        );
    },
    getOdrderList() {
        let _this = this;
        util.get(getApp().api.follow.getOrderFollowList).then(
            function (res) {
                console.log(res);
                if (res.code == 200) {
                    if(res.data.length>20){
                        res.data = res.data.slice(0,20)
                    }
                    _this.setData({
                        list:res.data ||[]
                    });
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function () {
                        wx.hideToast();
                    }, 1500);
                }
            }
        );
    },
    getUserInfo: function (e) {
        this.setData({
            userInfo: e.detail.userInfo
        })
    },
    toCoupon: function () {
        if (this.data.userInfo) {
        } else {
            this.login.openLoginModal()
        }
    },
    closeListener: () => {
        console.log('我要跳转页面了')
    },
    loginListener: () => {
        let pages = getCurrentPages()
        pages[pages.length - 1].onLoad()
    },
    closeOptions: function () {
        this.setData({
            isShowOptions: !1
        })
    },
    closeOptionsRedPacket: function () {
        this.setData({
            isShowRedPacket: !1
        })
    },

    onShow: function () {
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
    }
})
